# STEP 1: OBSERVATIONAL DATA ON VALUE OF POLITICAL INVOLVEMENT 
# DATA COLLECTED SUMMER OF 2014

# ---- Produces Table 1 and Appendix Table 1A

#-------Load Packages------#
require(foreign)
require(xtable)
require(car)
require(lattice)
require(MASS)
require(plotrix)
require(survey)
require(gmodels)
require(doBy)
require(ggplot2)
require(plyr)
require("boot") 
require(plotrix)
library(reshape)


#----- Load Files 
setwd("~/Box Sync/S_CollectiveActionPaper/Chapter2/PaperDrafts/Submissions/APSR_3_FinalSubmit/Replication/Data/SlimData/")

data <- read.csv("1Observational_ValueParticipation_Slim.csv", stringsAsFactors=FALSE, fileEncoding="latin1")
names(data)


# ------ Make Weights
data$id = seq(1,nrow(data))
# - Recode demographics age, education, ... to match CPS categories

data$age = rep(NA,dim(data)[1])
data$age[data$demAgeFull<=13]<-1
data$age[data$demAgeFull>=14&data$demAgeFull<=28]<-2
data$age[data$demAgeFull>=29&data$demAgeFull<=48]<-3
data$age[data$demAgeFull>=49]<-4
levels(data$age) = c("Age: 18-29","Age: 30-44","Age: 45-64","Age: 65+")

## Age Seven Part
data$age7 = rep(NA,dim(data)[1])
data$age7[data$demAgeFull<=8]<-1 #18-24
data$age7[data$demAgeFull>=9&data$demAgeFull<=18]<-2 #25-34
data$age7[data$demAgeFull>=19&data$demAgeFull<=28]<-3 #35-44
data$age7[data$demAgeFull>=29&data$demAgeFull<=38]<-4 #45-54
data$age7[data$demAgeFull>=39&data$demAgeFull<=48]<-5 #55-64
data$age7[data$demAgeFull>=49&data$demAgeFull<=58]<-6 #65-74
data$age7[data$demAgeFull>=59]<-7 #75+
levels(data$age7) = c("18-24","25-34","35-44","45-54","55-64","65-74","75+")

#Education
## Use four part age - HS or less, Some Coll, Coll Grad, Post+
data$educ4 = rep(NA,dim(data)[1])
data$educ4[data$demEduFull==1] =1
data$educ4[data$demEduFull==2] =1
data$educ4[data$demEduFull==3] =1
data$educ4[data$demEduFull==4] =2
data$educ4[data$demEduFull==5] =2
data$educ4[data$demEduFull==6] =2
data$educ4[data$demEduFull==7] =3
data$educ4[data$demEduFull==8] =4
data$educ4[data$demEduFull==9] =4
levels(data$educ4) = c("HS or less","Some College","College Grad","Postgraduate")

## Use five part age - lths, HS grad, Some Coll, Coll Grad, Post+
data$educ5 = rep(NA,dim(data)[1])
data$educ5[data$demEduFull==1] =1
data$educ5[data$demEduFull==2] =1
data$educ5[data$demEduFull==3] =2
data$educ5[data$demEduFull==4] =3
data$educ5[data$demEduFull==5] =3
data$educ5[data$demEduFull==6] =3
data$educ5[data$demEduFull==7] =4
data$educ5[data$demEduFull==8] =5
data$educ5[data$demEduFull==9] =5
levels(data$educ5) = c("LTHS","HS grad","Some College","College Grad","Postgraduate")

## college
data$college = rep(NA,dim(data)[1])
data$college[data$educ4<=2] =0
data$college[data$educ4>2] =1
levels(data$college) = c("Not College Grad","College Grad")

## Census Nine Divisions (http://en.wikipedia.org/wiki/List_of_regions_of_the_United_States#Census_Bureau-designated_areas)
data$div9<-rep(NA,dim(data)[1]) #four regions - northeast, midwest, south, west
data$div9[data$demState %in% c(20,30,46,22,40,7)]<-1
data$div9[data$demState %in% c(33,39,31)]<-2
data$div9[data$demState %in% c(50,23,14,15,36)]<-3
data$div9[data$demState %in% c(26,35,42,28,24,17,16)]<-4
data$div9[data$demState %in% c(8,9,21,47,49,41,34,10,11)]<-5
data$div9[data$demState %in% c(18,43,25,1)]<-6
data$div9[data$demState %in% c(4,19,37,44)]<-7
data$div9[data$demState %in% c(13,27,51,29,45,6,3,32)]<-8
data$div9[data$demState %in% c(2,48,38,12,5)]<-9
levels(data$div9)=c("New England","Mid-Atlantic","East North Central","West North Central",
                    "South Atlantic","East South Central","West South Central","Mountain","Pacific")

## Census Four Regions (http://en.wikipedia.org/wiki/List_of_regions_of_the_United_States#Census_Bureau-designated_areas)
data$reg4<-rep(NA,dim(data)[1]) #four regions - northeast, midwest, south, west
data$reg4[data$div9 %in% c(1,2)]<-1
data$reg4[data$div9 %in% c(3,4)]<-2
data$reg4[data$div9 %in% c(5,6,7)]<-3
data$reg4[data$div9 %in% c(8,9)]<-4
levels(data$reg4) = c("Northeast","Midwest","South","West")

data$race_eth = rep(NA,dim(data)[1])
data$race_eth[data$demRace==3&data$demHisp==2] =1 #non-hispanic black
data$race_eth[data$demRace==4&data$demHisp==2] =2 #non-hispanic white
data$race_eth[data$demHisp==1] =3 #hispanic
data$race_eth[data$demHisp!=1&(data$demRace==1|data$demRace==2|data$demRace==5)] =4 #other
levels(data$race_eth) = c("Black","Non-Hispanic White","Hispanic","Other")

data$white = ifelse(data$demRace==4,1,0)
data$ind_ideo = rep(NA,dim(data)[1])
data$ind_ideo[data$pid3==3&data$demPolIdeo>=5] =1 
data$ind_ideo[data$pid3==3&data$demPolIdeo<=3] =2
data$ind_ideo[data$pid3==3&data$demPolIdeo==4] =3 
levels(data$ind_ideo) = c("Conservative Independent","Liberal Independent","Moderate Independent")

data$matchIncome = recode(data$demInc,"1=1;2:3=2;4=3;5=4;6=5")
data$matchMarital = recode(data$demMarital,"1=1;6=1;2=2;3=3;4=4;5=5;else=NA")

## pid3
data$pid3 = rep(NA,dim(data)[1])
data$pid3[data$demPidNoLn==1] =1
data$pid3[data$demPidNoLn==2] =2
data$pid3[data$demPidNoLn>=3] =3
levels(data$pid3) = c("Republican","Democrat","Independent")


# Slim on Complete Cases of Weight Variables 
cc <- complete.cases(data[c("demHisp","demGender", "demRace","age","educ5","div9","matchIncome","demOwn","matchMarital","pid3")])
n=length(which(cc))
data <- data[cc,]


##---- set population parameters (from current population survey 2012)
pop.gender <- data.frame(demGender=c("2","1"), Freq=c(0.5337213*n,0.4662787*n))  #2012 cps
pop.age <- data.frame(age7=c("1","2","3","4","5","6","7"), Freq=c(0.09641236*n,0.15301476*n,0.15971343*n,0.19225408*n,0.18510355*n,0.12157431*n,0.09192751*n))  #2012 cps
pop.hisp <- data.frame(demHisp=c("1","2"), Freq=c(0.0891*n,0.91056667*n))  #2012 cps
pop.race <- data.frame(demRace=c("1","2","3","4","5"), Freq=c(0.008466552*n,0.032779701*n,0.128579846*n,0.814366970*n,0.015806931*n))  #2012 cps
pop.educ <- data.frame(educ5=c("1","2","3","4","5"), Freq=c(0.07157311*n,0.27194299*n,0.31378242*n,0.22209648*n,0.12060500*n))  #2012 cps
pop.region <- data.frame(div9=c("1","2","3","4","5","6","7","8","9"), Freq=c(0.05200688*n,0.13035183*n,0.16134013*n,0.07463418*n,0.19986425*n,0.06442249*n,0.10739053*n,0.06743083*n,0.14255888*n))  #2012 cps
pop.inc <- data.frame(matchIncome=c("1","2","3","4","5"), Freq=c(0.1407195*n,0.2929958*n,0.1991167*n,0.1296495*n,0.2375185*n))  #2012 cps
pop.own <- data.frame(demOwn=c("1","2","3"), Freq=c(0.7552340*n,0.23407120*n,0.01111521*n))  #2012 cps
pop.marital <- data.frame(matchMarital=c("1","2","3","4","5"), Freq=c(0.23210460*n,0.579341555*n,0.01957337*n,0.10767534*n,0.06589381*n))  #2012 cps
pop.pid3 <- data.frame(pid3=c("1","2","3"), Freq=c(0.33*n,0.37*n,0.30*n)) 



data$wts = rep(1,dim(data)[1])
names(data)

# --- Construct Weight
##Post-stratification - close to population estimates


design0 <- suppressWarnings(svydesign(id=~1, data=data))

designDemWt <- rake(design0, sample.margins=list(~demHisp,~demGender, ~age7,~demRace,~educ5,~div9,~matchIncome,~demOwn,~matchMarital), 
                    population.margins=list(pop.hisp,pop.gender, pop.age,pop.race,pop.educ,pop.region,pop.inc,pop.own,pop.marital),
                    control=list(epsilon=.00001, maxit=100, verbose=FALSE))

data$wts = weights(designDemWt)

names(data)
summary(data$wts)


#Make single race variable
#Hispanic: demHisp
	#1 = Yes
	#2 - No
#Race: demRace
	#1 = American Indian
	#2 = Asian American
	#3 = Black
	#4 = White
	#5 = Other
data$race<- rep(NA, length(data$Resp.ID))
data$race[data$demRace == 4 & data$demHisp == 2] <- 1 #White, not hispanic
data$race[data$demRace == 3 & data$demHisp == 2] <- 2 #Black, not hispanic
data$race[data$demHisp == 1] <- 3 #Hispanic, any race
data$race[data$demRace == 2 & data$demHisp == 2] <- 4 #Asian, not hispanic
table(data$race)
#1 	White, not hispanic
#2	Black, not hispanic
#3	Hispanic, any race
#4	Asian, not hispanic

#Race with labels
data$race_l<- rep(NA, length(data$Resp.ID))
data$race_l[data$demRace == 4 & data$demHisp == 2] <- "White" #White, not hispanic
data$race_l[data$demRace == 3 & data$demHisp == 2] <- "Black" #Black, not hispanic
data$race_l[data$demHisp == 1] <- "Hispanic" #Hispanic, any race
data$race_l[data$demRace == 2 & data$demHisp == 2] <- "Asian" #Asian, not hispanic
table(data$race_l)



#------- Examine Means
#----Calculate weighted mean and 95% confidence intervals

sample.wtd.mean <- function(x, wb, d) {
    return(weighted.mean(x = x[d], w = wb[d], na.rm=T ))
} #boot function for calculating weighted mean



#-------- Voting Grid
# Voting make my life better
v.1 <- ddply(.data = data, .variables= .(race_l), .fun = summarise,
        mean = weighted.mean(x=Q129_1, w=wts, na.rm=T),
        se = sd(boot(Q129_1, sample.wtd.mean, R = 1000, w = wts)$t))
#Remove final row of dataframe
v.1 <-v.1[-5,]
v.1

# Voting make my community better
v.2 <- ddply(.data = data, .variables= .(race_l), .fun = summarise,
        mean = weighted.mean(x=Q129_2, w=wts, na.rm=T),
        se = sd(boot(Q129_2, sample.wtd.mean, R = 1000, w = wts)$t))
#Remove final row of dataframe
v.2 <-v.2[-5,]
v.2

# People who vote are good community members
v.3 <- ddply(.data = data, .variables= .(race_l), .fun = summarise,
        mean = weighted.mean(x=Q129_3, w=wts, na.rm=T),
        se = sd(boot(Q129_3, sample.wtd.mean, R = 1000, w = wts)$t))
#Remove final row of dataframe
v.3 <-v.3[-5,]
v.3

# Voting helps people in need
v.4 <- ddply(.data = data, .variables= .(race_l), .fun = summarise,
        mean = weighted.mean(x=Q129_4, w=wts, na.rm=T),
        se = sd(boot(Q129_4, sample.wtd.mean, R = 1000, w = wts)$t))
#Remove final row of dataframe
v.4 <-v.4[-5,]
v.4



#-------- Rally Grid
# Political rallies make my life better
r.1 <- ddply(.data = data, .variables= .(race_l), .fun = summarise,
        mean = weighted.mean(x=Q127_1, w=wts, na.rm=T),
        se = sd(boot(Q127_1, sample.wtd.mean, R = 1000, w = wts)$t))
#Remove final row of dataframe
r.1 <-r.1[-5,]
r.1


# Political rallies make my community better
r.2 <- ddply(.data = data, .variables= .(race_l), .fun = summarise,
        mean = weighted.mean(x=Q127_2, w=wts, na.rm=T),
        se = sd(boot(Q127_2, sample.wtd.mean, R = 1000, w = wts)$t))
#Remove final row of dataframe
r.2 <-r.2[-5,]
r.2


# People who attend political rallies are good community members
r.3 <- ddply(.data = data, .variables= .(race_l), .fun = summarise,
        mean = weighted.mean(x=Q127_3, w=wts, na.rm=T),
        se = sd(boot(Q127_3, sample.wtd.mean, R = 1000, w = wts)$t))
#Remove final row of dataframe
r.3 <-r.3[-5,]
r.3

# Attending political rallies helps people in need
r.4 <- ddply(.data = data, .variables= .(race_l), .fun = summarise,
        mean = weighted.mean(x=Q127_4, w=wts, na.rm=T),
        se = sd(boot(Q127_4, sample.wtd.mean, R = 1000, w = wts)$t))
#Remove final row of dataframe
r.4 <-r.4[-5,]
r.4





#------- Create Table 1
#-------- Voting 
#Remove Asians
	v2 <- v.1[-1,]
	v2
#Add indicator
	v2$q <- "my life better"
	v2
#Remove se column
v2.1 <- v2[-c(3)]
v2.1
#Reshape 
	require(reshape)
	v2.2 <- melt(v2.1, id=c("race_l", "q"))
	v2.2
	v2.3 <- cast(v2.2, q ~ race_l )
	v2.3
#Round table	
	v2.3[,-1] <-  round(v2.3[,-1], 2)
	v2.3


#Q2: Voting in presidential elections make my community better.
#Round table	
	v3 <- v.2[-1,]
	v3
#Add indicator
	v3$q <-   "my community better"
	v3
#Remove se column
	v3.1 <- v3[-c(3)]
	v3.1
#Reshape 
	require(reshape)
	v3.2 <- melt(v3.1, id=c("race_l", "q"))
	v3.2
	v3.3 <- cast(v3.2, q ~ race_l )
	v3.3
#Round table	
	v3.3[,-1] <-  round(v3.3[,-1], 2)
	v3.3

#Q3: Voting = good community member.
#Round table	
	v4 <- v.3[-1,]
	v4
#Add indicator
	v4$q <-   "good community member"
	v4
#Remove se column
	v4.1 <- v4[-c(3)]
	v4.1
#Reshape 
	require(reshape)
	v4.2 <- melt(v4.1, id=c("race_l", "q"))
	v4.2
	v4.3 <- cast(v4.2, q ~ race_l )
	v4.3
#Round table	
	v4.3[,-1] <-  round(v4.3[,-1], 2)
	v4.3
	
#Q4: Voting in presidential elections helps people in need
#Round table	
	v5 <- v.4[-1,]
	v5
#Add indicator
	v5$q <-    "helps people in need"
	v5
#Remove se column
	v5.1 <- v5[-c(3)]
	v5.1
#Reshape 
	require(reshape)
	v5.2 <- melt(v5.1, id=c("race_l", "q"))
	v5.2
	v5.3 <- cast(v5.2, q ~ race_l )
	v5.3
#Round table	
	v5.3[,-1] <-  round(v5.3[,-1], 2)
	v5.3

#  Combine means into single table
vote.means <- rbind(v2.3, v3.3, v4.3, v5.3)
vote.means
#Reorder columns
vote.means <- vote.means[c(1,4,2,3)]
vote.means


#Merge rally means into a single table 
#Q1: rally in presidential elections make my life better.
#Remove Asians
	r2 <- r.1[-1,]
	r2
#Add indicator
	r2$q <- "my life better"
	r2
#Remore se column
r2.1 <- r2[-c(3)]
r2.1
#Reshape 
	require(reshape)
	r2.2 <- melt(r2.1, id=c("race_l", "q"))
	r2.2
	r2.3 <- cast(r2.2, q ~ race_l )
	r2.3
#Round table	
	r2.3[,-1] <-  round(r2.3[,-1], 2)
	r2.3

#Q2: Rally in presidential elections make my community better.
#Round table	
	r3 <- r.2[-1,]
	r3
#Add indicator
	r3$q <-   "my community better"
	r3
#Remore se column
	r3.1 <- r3[-c(3)]
	r3.1
#Reshape 
	require(reshape)
	r3.2 <- melt(r3.1, id=c("race_l", "q"))
	r3.2
	r3.3 <- cast(r3.2, q ~ race_l )
	r3.3
#Round table	
	r3.3[,-1] <-  round(r3.3[,-1], 2)
	r3.3

#Q3: Rally = good community member.
#Round table	
	r4 <- r.3[-1,]
	r4
#Add indicator
	r4$q <-   "good community member"
	r4
#Remore se column
	r4.1 <- r4[-c(3)]
	r4.1
#Reshape 
	require(reshape)
	r4.2 <- melt(r4.1, id=c("race_l", "q"))
	r4.2
	r4.3 <- cast(r4.2, q ~ race_l )
	r4.3
#Round table	
	r4.3[,-1] <-  round(r4.3[,-1], 2)
	r4.3

#Q4: Rally in presidential elections helps people in need
#Round table	
	r5 <- r.4[-1,]
	r5
#Add indicator
	r5$q <-    "helps people in need"
	r5
#Remore se column
	r5.1 <- r5[-c(3)]
	r5.1
#Reshape 
	require(reshape)
	r5.2 <- melt(r5.1, id=c("race_l", "q"))
	r5.2
	r5.3 <- cast(r5.2, q ~ race_l )
	r5.3
#Round table	
	r5.3[,-1] <-  round(r5.3[,-1], 2)
	r5.3

#  Combine means into single table
rally.means <- rbind(r2.3, r3.3, r4.3, r5.3)
rally.means
#Reorder columns
rally.means <- rally.means[c(1,4,2,3)]
rally.means



# -- For Latex -- Table 1
print(xtable(vote.means), include.rownames=FALSE)
print(xtable(rally.means), include.rownames=FALSE)



#Significant Tests -- Results Added Manually to Table 1
require(weights)

#VOTING: Black
#Life Better
wtd.t.test(
			data$Q129_1[data$race_l=="Black"], 
			data$Q129_1[data$race_l=="White"], 
			weight=data$wts[data$race_l=="Black"],
			weighty=data$wts[data$race_l=="White"],
			samedata=FALSE)
#Community Better
wtd.t.test(
			data$Q129_2[data$race_l=="Black"], 
			data$Q129_2[data$race_l=="White"], 
			weight=data$wts[data$race_l=="Black"],
			weighty=data$wts[data$race_l=="White"],
			samedata=FALSE)
#Communtiy Memebers
wtd.t.test(
			data$Q129_3[data$race_l=="Black"], 
			data$Q129_3[data$race_l=="White"], 
			weight=data$wts[data$race_l=="Black"],
			weighty=data$wts[data$race_l=="White"],
			samedata=FALSE)
#Help people in need
wtd.t.test(
			data$Q129_4[data$race_l=="Black"], 
			data$Q129_4[data$race_l=="White"], 
			weight=data$wts[data$race_l=="Black"],
			weighty=data$wts[data$race_l=="White"],
			samedata=FALSE)
#VOTING: Hispanic
#Life Better
wtd.t.test(
			data$Q129_1[data$race_l=="Hispanic"], 
			data$Q129_1[data$race_l=="White"], 
			weight=data$wts[data$race_l=="Hispanic"],
			weighty=data$wts[data$race_l=="White"],
			samedata=FALSE)
#Community Better
wtd.t.test(
			data$Q129_2[data$race_l=="Hispanic"], 
			data$Q129_2[data$race_l=="White"], 
			weight=data$wts[data$race_l=="Hispanic"],
			weighty=data$wts[data$race_l=="White"],
			samedata=FALSE)
#Communtiy Memebers
wtd.t.test(
			data$Q129_3[data$race_l=="Hispanic"], 
			data$Q129_3[data$race_l=="White"], 
			weight=data$wts[data$race_l=="Hispanic"],
			weighty=data$wts[data$race_l=="White"],
			samedata=FALSE)
#Help people in need
wtd.t.test(
			data$Q129_4[data$race_l=="Hispanic"], 
			data$Q129_4[data$race_l=="White"], 
			weight=data$wts[data$race_l=="Hispanic"],
			weighty=data$wts[data$race_l=="White"],
			samedata=FALSE)
#VOTING: Minority Comparison
#Life Better
wtd.t.test(
			data$Q129_1[data$race_l=="Hispanic"], 
			data$Q129_1[data$race_l=="Black"], 
			weight=data$wts[data$race_l=="Hispanic"],
			weighty=data$wts[data$race_l=="Black"],
			samedata=FALSE)
#Community Better
wtd.t.test(
			data$Q129_2[data$race_l=="Hispanic"], 
			data$Q129_2[data$race_l=="Black"], 
			weight=data$wts[data$race_l=="Hispanic"],
			weighty=data$wts[data$race_l=="Black"],
			samedata=FALSE)
#Communtiy Memebers
wtd.t.test(
			data$Q129_3[data$race_l=="Hispanic"], 
			data$Q129_3[data$race_l=="Black"], 
			weight=data$wts[data$race_l=="Hispanic"],
			weighty=data$wts[data$race_l=="Black"],
			samedata=FALSE)
#Help people in need
wtd.t.test(
			data$Q129_4[data$race_l=="Hispanic"], 
			data$Q129_4[data$race_l=="Black"], 
			weight=data$wts[data$race_l=="Hispanic"],
			weighty=data$wts[data$race_l=="Black"],
			samedata=FALSE)



#----- Examining Significant to Add to table
#RALLY: Black
#Life Better
wtd.t.test(
			data$Q127_1[data$race_l=="Black"], 
			data$Q127_1[data$race_l=="White"], 
			weight=data$wts[data$race_l=="Black"],
			weighty=data$wts[data$race_l=="White"],
			samedata=FALSE)
#Community Better
wtd.t.test(
			data$Q127_2[data$race_l=="Black"], 
			data$Q127_2[data$race_l=="White"], 
			weight=data$wts[data$race_l=="Black"],
			weighty=data$wts[data$race_l=="White"],
			samedata=FALSE)
#Communtiy Memebers
wtd.t.test(
			data$Q127_3[data$race_l=="Black"], 
			data$Q127_3[data$race_l=="White"], 
			weight=data$wts[data$race_l=="Black"],
			weighty=data$wts[data$race_l=="White"],
			samedata=FALSE)
#Help people in need
wtd.t.test(
			data$Q127_4[data$race_l=="Black"], 
			data$Q127_4[data$race_l=="White"], 
			weight=data$wts[data$race_l=="Black"],
			weighty=data$wts[data$race_l=="White"],
			samedata=FALSE)
#RALLY: Hispanic
#Life Better
wtd.t.test(
			data$Q127_1[data$race_l=="Hispanic"], 
			data$Q127_1[data$race_l=="White"], 
			weight=data$wts[data$race_l=="Hispanic"],
			weighty=data$wts[data$race_l=="White"],
			samedata=FALSE)
#Community Better
wtd.t.test(
			data$Q127_2[data$race_l=="Hispanic"], 
			data$Q127_2[data$race_l=="White"], 
			weight=data$wts[data$race_l=="Hispanic"],
			weighty=data$wts[data$race_l=="White"],
			samedata=FALSE)
#Communtiy Memebers
wtd.t.test(
			data$Q127_3[data$race_l=="Hispanic"], 
			data$Q127_3[data$race_l=="White"], 
			weight=data$wts[data$race_l=="Hispanic"],
			weighty=data$wts[data$race_l=="White"],
			samedata=FALSE)
#Help people in need
wtd.t.test(
			data$Q127_4[data$race_l=="Hispanic"], 
			data$Q127_4[data$race_l=="White"], 
			weight=data$wts[data$race_l=="Hispanic"],
			weighty=data$wts[data$race_l=="White"],
			samedata=FALSE)
#RALLY: Minority Comparison
#Life Better
wtd.t.test(
			data$Q127_1[data$race_l=="Hispanic"], 
			data$Q127_1[data$race_l=="Black"], 
			weight=data$wts[data$race_l=="Hispanic"],
			weighty=data$wts[data$race_l=="Black"],
			samedata=FALSE)
#Community Better
wtd.t.test(
			data$Q127_2[data$race_l=="Hispanic"], 
			data$Q127_2[data$race_l=="Black"], 
			weight=data$wts[data$race_l=="Hispanic"],
			weighty=data$wts[data$race_l=="Black"],
			samedata=FALSE)
#Communtiy Memebers
wtd.t.test(
			data$Q127_3[data$race_l=="Hispanic"], 
			data$Q127_3[data$race_l=="Black"], 
			weight=data$wts[data$race_l=="Hispanic"],
			weighty=data$wts[data$race_l=="Black"],
			samedata=FALSE)
#Help people in need
wtd.t.test(
			data$Q127_4[data$race_l=="Hispanic"], 
			data$Q127_4[data$race_l=="Black"], 
			weight=data$wts[data$race_l=="Hispanic"],
			weighty=data$wts[data$race_l=="Black"],
			samedata=FALSE)




#----Standard Deviation of Variables -- For in-text discussion of magnitude of results
require(Hmisc)

data_BW2 <- subset(data, race_l=="White" | race_l=="Black")
table(data_BW2$race_l)

#Mean and Variance for Black - White Sample
#Voting
xm <- wtd.mean(data_BW2$Q127_1, data_BW2$wts)
xm
var <- wtd.var(data_BW2$Q127_1, data_BW2$wts)
var
sd <- sqrt(var)
sd

xm <- wtd.mean(data$Q127_4, data$wts)
xm
var <- wtd.var(data$Q127_4, data$wts)
var
sd <- sqrt(var)
sd

0.62/sd

xm <- wtd.mean(data$Q127_2, data$wts)
xm
var <- wtd.var(data$Q127_2, data$wts)
var
sd <- sqrt(var)
sd

0.53/sd


xm <- wtd.mean(data$Q127_2, data$wts)
xm
var <- wtd.var(data$Q127_2, data$wts)
var
sd <- sqrt(var)
sd


xm <- wtd.mean(data$Q127_3, data$wts)
xm
var <- wtd.var(data$Q127_3, data$wts)
var
sd <- sqrt(var)
sd
0.31/sd



xm <- wtd.mean(data$Q129_4, data$wts)
xm
var <- wtd.var(data$Q129_4, data$wts)
var
sd <- sqrt(var)
sd

0.43/sd






# --------- APPENDIX MATERIAL 
# ------- Differences in Norms with Covariations
#---------Linear Regression Models -- Plus covariates 

#Covariates to Include
table(data$demGender)
table(data$pid3, data$race)

#recode Party ID
#R=1, D=2, I=3
data$pid2 <- recode(data$pid3, "1=1; 2=0; else=NA")
table(data$pid2)
table(data$pid3)

#Independent basline, R=1, Dem = 2
data$pid3_ibase <- rep(NA, length(data$Resp.ID))
data$pid3_ibase <- recode(data$pid3, "1=1; 2=2; 3=0; else=NA")

data$pid3_dbase <- rep(NA, length(data$Resp.ID))
data$pid3_dbase <- recode(data$pid3, "1=1; 2=0; 3=2; else=NA")

#recode race, drope Asian 
data$race_3<- rep(NA, length(data$Resp.ID))
data$race_3 <- recode(data$race, "1=1; 2=2; 3=3; else=NA")
table(data$race_3)


#Recode Ideology
table(data$demPolIdeo)
data$ideo<- rep(NA, length(data$Resp.ID))
data$ideo <- recode(data$demPolIdeo, "1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA")
table(data$ideo)



#--- Voting 
#Q129_1 - Q129_4
#estimates grow with increasing precision when a list of known-covariates (maybe even what race as a social construction is) is added into the regression. Race is the largest predictor by orders of...
v.life <- lm(Q129_1 ~ as.factor(race_3)+as.factor(pid3_ibase) + educ5 +  matchIncome +age7 +demGender, data=data, weights=wts)
summary(v.life)
v.com <- lm(Q129_2 ~ as.factor(race_3)+as.factor(pid3_ibase) + educ5 +  matchIncome +age7 +demGender, data=data, weights=wts)
summary(v.com)
v.mem <- lm(Q129_3 ~ as.factor(race_3)+as.factor(pid3_ibase) + educ5 +  matchIncome +age7 +demGender, data=data, weights=wts)
summary(v.mem)
v.help <- lm(Q129_4 ~ as.factor(race_3)+as.factor(pid3_ibase) + educ5 +  matchIncome +age7 +demGender, data=data, weights=wts)
summary(v.help)


#---- Rally
r.life <- lm(Q127_1 ~ as.factor(race_3)+as.factor(pid3_ibase) + educ5 +  matchIncome +age7 +demGender, data=data, weights=wts)
summary(r.life)
r.com <- lm(Q127_2 ~ as.factor(race_3)+as.factor(pid3_ibase) + educ5 +  matchIncome +age7 +demGender, data=data, weights=wts)
summary(r.com)
r.mem <- lm(Q127_3 ~ as.factor(race_3)+as.factor(pid3_ibase) + educ5 +  matchIncome +age7 +demGender, data=data, weights=wts)
summary(r.mem)
r.help <- lm(Q127_4 ~ as.factor(race_3)+as.factor(pid3_ibase) + educ5 +  matchIncome +age7 +demGender, data=data, weights=wts)
summary(r.help)


#---- Make Table A1 -- Appendix
require(texreg)
texreg(list(v.life, v.com, v.help, v.mem, r.life, r.com, r.help, r.mem),
	custom.coef.names = c( 
	"Intercept",
	"Black", 
	"Latino",
	"Republican",
	"Democrat",
	"Education",
	"Income",
	"Age",
	"Gender"
	),
	custom.model.names = c("Life Better", "Community Better", "Helps Needy", "Good Members", "Life Better", "Community Better", "Helps Needy", "Good Members"),
	 dcolumn=TRUE, booktabs = TRUE, use.packages = FALSE, label ="tab:regTable",  stars = c(0.05),   caption = "Beliefs About the Value of Participation with Covariates", float.pos = "hb", single.row = TRUE) #change significane levels: stars = c(0.01, 0.001)






